package wili.b1.transform;

import java.io.File;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import wili.attrMapping.file.JsonFile;
import wili.c.Ca;
import wili.json.JsonArr;
import wili.json.JsonNode;

/**
 *
 */
public class SortJsonAttrs {
	public static void main(String[] args) {
		File dir = new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\fragment");
		//"a1/acc_819c2c615aar.json")
		JsonFile ff = new JsonFile(new File(dir, "a1/acc_US.json"));
		JsonFile tt = new JsonFile(new File(dir, "accountingTab.fragment.json"));
		ff.findNodes(new Predicate<JsonNode>() {
			@Override
			public boolean test(JsonNode jsonNode) {
				//jsonNode instanceof JsonObj ||
				if (jsonNode instanceof JsonArr) {
					JsonNode id = jsonNode.dad.son("guid");
					if (id == null)
						return false;
					Ca.log("guid=" + id);
					//375c543b-95c7-4b5a-8f95-99118a0cbcfb
					JsonNode ret = tt.findObj_byAtt("guid", id.stringValue);
					if (ret == null) {
						return false;
					}
					ret = ret.son(jsonNode.attName);
					String st1 = ret.sons().toString();
					List<String> lis = jsonNode.sons().stream().map(nod -> nod.son("guid").stringValue)
							.collect(Collectors.toList());
					ret.sortSons(lis);
					;
					//ret.sortSons(jsonNode.sons().stream().map(nod -> (nod.attName == null ? String.valueOf(nod.index()) : nod.attName)).collect(Collectors.toList()));
					String st2 = ret.sons().toString();
					if (!st1.equals(st2)) {
						Ca.pause();
					}
				}
				return false;
			}
		});
		tt.saveToFile(null, null);
	}
}
